package la.bbs558.controller.admin;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import la.bbs558.controller.base.Result;
import la.bbs558.entity.AdContent;
import la.bbs558.entity.enums.OssFolder;
import la.bbs558.entity.params.AdContentSearchPage;
import la.bbs558.entity.params.SendPostForAdminParams;
import la.bbs558.satoken.StpAdminUtil;
import la.bbs558.service.AdContentService;
import la.bbs558.util.Objs;
import la.bbs558.util.OssUtil;
import la.bbs558.util.WordUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: Shanks
 * @date: 2023/12/15 10:45
 * @desc:
 */
@RestController
@RequestMapping("/admin/ad")
@RequiredArgsConstructor
@SaCheckRole(value = { "admin" },type = StpAdminUtil.TYPE)
public class AdManageController {

    private final AdContentService adContentService;

    private final OssUtil ossUtil;
    private final WordUtil wordUtil;

    @PostMapping("delete")
    public Result<?> deleteAdContentById(Long id) {
        Objs.isTrue(id != null, "id不能为空");
        adContentService.deleteAdContentForAdmin(id);
        return Result.success();
    }

    @PostMapping("upload")
    public Result<?> uploadAdImage(MultipartFile file) {
        String key = ossUtil.uploadFile(file, OssFolder.Ad);
        return Result.success(key);
    }

    @PostMapping("add")
    public Result<?> addAdContent(@RequestBody AdContent adContent) {
        adContentService.addAdContentForAdmin(adContent);
        return Result.success();
    }

    @PostMapping("update")
    public Result<?> updateAdContent(@RequestBody AdContent adContent) {
        adContentService.updateAdContentForAdmin(adContent);
        return Result.success();
    }

    @GetMapping("list")
    public Result<?> list(AdContentSearchPage searchPage) {
        adContentService.selectAdContentPageForAdmin(searchPage);
        return Result.success(searchPage);
    }

    @GetMapping("covert2RichText")
    @SaIgnore
    public Result<?> covert2RichText(MultipartFile file) {
        String richText = wordUtil.covert2RichText(new SendPostForAdminParams().setFile(file));
        return Result.success(richText);
    }
}
